package com.translate.web.service.impl;

import com.common.util.CopyEntity;
import com.translate.web.constant.RedisConstant;
import com.translate.web.dao.OrderPicMapper;
import com.translate.entity.ContractPic;
import com.translate.entity.CustomerPoolMember;
import com.translate.entity.OrderPic;
import com.translate.web.param.contract.IContractPicAddParam;
import com.param.contract.IIdParam;
import com.translate.web.param.contract.IOrderPicAddParam;
import com.translate.web.service.IOrderPicService;
import com.basic.service.impl.BaseServiceImpl;
import com.translate.vo.contract.IOrderPicVO;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;


@Service
public class OrderPicServiceImpl extends BaseServiceImpl<OrderPic> implements IOrderPicService {
	
	private RedisTemplate<String, List<OrderPic>> redisTemplate;
	
	
	
	
	public OrderPicServiceImpl(OrderPicMapper mapper) {
		super(mapper);
	}

	protected Class<OrderPic> getEntityClass() {
		return OrderPic.class;
	}

	@Override
	public int batchInsert(String[] list, int orderId) {
		
		if (null == list) {
			return 0;
		}
		
		List<OrderPic> ts = new ArrayList<>(); 
		
		OrderPic pic;
		
		for (String s : list) {
			
			pic = new OrderPic();
			
			pic.setOrderId(orderId);
			
			pic.setPic(s);
			
			ts.add(pic);
		}
		
		return this.batchInsert(ts);
	}

	@Override
	public <VO extends IOrderPicVO> List<VO> getPics(int orderId, Class<VO> target) {
		
		List<VO> vos = new ArrayList<>();
		
		VO vo;
		
		List<OrderPic> orderPics = this.getOrderPicsToCache(orderId);
		
		try {
			
			for (OrderPic orderPic : orderPics) {
				
				vo = target.newInstance();
				
				vo.setOrderId(orderPic.getOrderId());
				
				vo.setId(orderPic.getId());
				
				vo.setPic(orderPic.getPic());
				
				vos.add(vo);
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	
		return vos;
		
	}
	
	/**
	 * 获取订单图片备注
	 * @param orderId
	 * @return
	 */
	protected List<OrderPic> getOrderPicsToCache(int orderId) {
		
		String key = RedisConstant.getOrderPicKey(orderId);
		
		ValueOperations<String, List<OrderPic>> valueOperations = this.redisTemplate.opsForValue();
		
		List<OrderPic> orderPics = valueOperations.get(key);
		
		if (null != orderPics) {
			return orderPics;
		}
		
		orderPics = this.getDataByIds(orderId);
		
		if (null == orderPics) {
			orderPics = new ArrayList<>(0);
			
			valueOperations.set(key, orderPics, 3, TimeUnit.SECONDS);
			
			return orderPics;
		}
		
		valueOperations.set(key, orderPics, 15, TimeUnit.SECONDS);

		return orderPics;
	}

	
	/**
	 * 比较更新主要条件
	 * 
	 * @return
	 */
	protected <S> boolean compareMasterCondition(S condition, OrderPic data) {

		IOrderPicAddParam customerPoolAddMemberParam = (IOrderPicAddParam) condition;


		String pic = customerPoolAddMemberParam.getPic();

		return data.getPic().equals(pic);
		
	}
	
	protected <S> void toEntity(OrderPic customerPoolMember, S obtainedFromTheMap, int assocId, int userId) {

		IOrderPicAddParam customerPoolAddMemberParam = (IOrderPicAddParam) obtainedFromTheMap;

		customerPoolMember.setOrderId(assocId);

		Integer id = null;
		
		if (obtainedFromTheMap instanceof IIdParam) {
			id = ((IIdParam) obtainedFromTheMap).getId();
			
			if (null != id) {
				customerPoolMember.setId(id);
			}
		}

		customerPoolMember.setPic(customerPoolAddMemberParam.getPic());
	}
	
	protected OrderPic getEntityInstance() {
		return new OrderPic();
	}

	protected int getId(OrderPic data) {
		return data.getId();
	}
	
	
	@Autowired
	public void setRedisTemplate(RedisTemplate<String, List<OrderPic>> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}
}